// pages/coupon/myCoupon/myCoupon.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        couponType: 1,
        couponList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getCouponList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getCouponList()
    },

    getCouponList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let couponList = this.data.couponList
        let data = {
            store_id: wx.getStorageSync('storeId'),
            status: this.data.couponType,
            limit: 10,
            page: this.data.nowPage,
            paramter: 1
        }
        common.post('Coupon/user_coupon_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    couponList: couponList,
                    isAll: true
                })
            } else {
                couponList = couponList.concat(res)
                that.setData({
                    couponList: couponList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (e.currentTarget.dataset.type == this.data.couponType) {
            return
        }
        this.setData({
            couponType: e.currentTarget.dataset.type,
            couponList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getCouponList()
    },

    goUse: function(e) {
        wx.navigateTo({
            url: '/pages/coupon/couponDetail/couponDetail?couponId=' + e.currentTarget.dataset.id
        })
    }
})